﻿/*-----------------------------------------------
// File: ChoseRoleModel.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Login
{
    public class ChoseRoleModel : MonoBehaviour
    {
        public ChoseRole choseRole;
        GameObject currentModel;
        GameObject boyModule;
        GameObject girlModule;

        bool isDragging = false;
        Vector3 startDragPos;

        public void OnChoseRole(ChoseRole.MainActorSex s)
        {
            StopAllCoroutines();
            StartCoroutine(ShowModel(s));
        }

        IEnumerator ShowModel(ChoseRole.MainActorSex s)
        {
            yield return StartCoroutine(LoadModel(s));
            if (s == ChoseRole.MainActorSex.Male)
            {
                if (boyModule != null)
                {
                    boyModule.SetActive(true);
                    boyModule.GetComponent<LegacyAnimation>().Play(AniName.WIN, WrapMode.Once);
                    StartCoroutine(PlayStand(boyModule));
                }
                if (girlModule != null)
                {
                    girlModule.SetActive(false);
                }
                currentModel = boyModule;
            }
            else if (s == ChoseRole.MainActorSex.Female)
            {
                if (girlModule != null)
                {
                    girlModule.SetActive(true);
                    girlModule.GetComponent<LegacyAnimation>().Play(AniName.WIN, WrapMode.Once);
                    StartCoroutine(PlayStand(girlModule));
                }
                if (boyModule != null)
                {
                    boyModule.SetActive(false);
                }
                currentModel = girlModule;
            }
        }

        IEnumerator LoadModel(ChoseRole.MainActorSex s)
        {
            if (s == ChoseRole.MainActorSex.Male)
            {
                if (boyModule != null)
                    yield break;
                var dh = Dict.Blo.DictHeroBlo.GetTableHero(ChoseRole.boyHeroId);
                yield return StartCoroutine(ResourceLoader.GetInstance().modelLoader.LoadModel(dh.resName, (asset) =>
                {
                    var obj = asset as GameObject;
                    if (obj != null)
                    {
                        LoadModelComplete(obj);
                        boyModule = obj;
                    }
                }));
                if (boyModule != null)
                {
                    boyModule.GetComponent<ModelRenderKit>().SwitchLod(false);
                    CommonMethod.ChangeLayer(boyModule.transform, LayerName.SceneActor);
                    yield return StartCoroutine(boyModule.GetComponent<ModelRenderKit>().LoadWeapons(dh.id, ChoseRole.equipId));
                }

            }
            else if (s == ChoseRole.MainActorSex.Female)
            {
                if (girlModule != null)
                    yield break;
                var dh = Dict.Blo.DictHeroBlo.GetTableHero(ChoseRole.girlHeroId);
                yield return StartCoroutine(ResourceLoader.GetInstance().modelLoader.LoadModel(dh.resName, (asset) =>
                {
                    var obj = asset as GameObject;
                    if (obj != null)
                    {
                        LoadModelComplete(obj);
                        girlModule = obj;
                    }
                }));
                if (girlModule != null)
                {
                    girlModule.GetComponent<ModelRenderKit>().SwitchLod(false);
                    CommonMethod.ChangeLayer(girlModule.transform, LayerName.SceneActor);
                    yield return StartCoroutine(girlModule.GetComponent<ModelRenderKit>().LoadWeapons(dh.id, ChoseRole.equipId));

                }
            }
        }

        void LoadModelComplete(GameObject heroAni)
        {
            heroAni.transform.SetParent(choseRole.transform);
            heroAni.transform.localRotation = new Quaternion(0, 180, 0, 0);
            heroAni.transform.localPosition = new Vector3(0, 0, -300);
            heroAni.transform.localScale = new Vector3(350, 350, 350);
        }

        IEnumerator PlayStand(GameObject module)
        {
            if (module != null)
            {
                float length = module.GetComponent<LegacyAnimation>().GetAniLength(AniName.WIN);
                yield return new WaitForSeconds(length);
                if (module != null)
                {
                    module.GetComponent<LegacyAnimation>().CrossFade(AniName.STAND, WrapMode.Loop);
                }
                else
                {
                    yield break;
                }
            }
            else
            {
                yield break;
            }
        }

        void Update()
        {
            if (currentModel != null && isDragging)
            {
                Vector3 v = Input.mousePosition;
                currentModel.transform.Rotate(new Vector3(0, (startDragPos.x - v.x) / 2, 0));
                startDragPos = v;
            }
        }

        public void OnHeroDragStart()
        {
            if (currentModel != null)
            {
                startDragPos = Input.mousePosition;
                isDragging = true;
            }
        }

        public void OnHeroDragEnd()
        {
            isDragging = false;
        }

        public void Unload()
        {
            StopAllCoroutines();
            if (boyModule != null)
            {
                var dh = Dict.Blo.DictHeroBlo.GetTableHero(ChoseRole.boyHeroId);
                boyModule.GetComponent<ModelRenderKit>().SwitchLod(true);
                ResourceLoader.GetInstance().modelLoader.UnloadModel(boyModule, dh.resName);
            }
            if (girlModule != null)
            {
                var dh = Dict.Blo.DictHeroBlo.GetTableHero(ChoseRole.girlHeroId);
                girlModule.GetComponent<ModelRenderKit>().SwitchLod(true);
                ResourceLoader.GetInstance().modelLoader.UnloadModel(girlModule, dh.resName);
            }
        }
    }
}
